namespace PMS.BLL
{
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class AdminOrganAuthorityBLL
    {
        public int Add(AdminOrganAuthority model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into AdminOrganAuthority(");
            builder.Append("AdminID,OrganID,IsAuthority");
            builder.Append(") values (");
            builder.Append("@AdminID,@OrganID,@IsAuthority");
            builder.Append(") ");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AdminID", model.AdminID), new SqlParameter("@OrganID", model.OrganID), new SqlParameter("@IsAuthority", model.IsAuthority) };
            object single = DBHelper.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public int AddIFNotExits(AdminOrganAuthority model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" IF NOT EXISTS(SELECT * FROM dbo.AdminOrganAuthority WHERE AdminID=@AdminID AND OrganID=@OrganID)");
            builder.Append(" BEGIN ");
            builder.Append(" insert into AdminOrganAuthority(");
            builder.Append(" AdminID,OrganID,IsAuthority");
            builder.Append(" ) values (");
            builder.Append(" @AdminID,@OrganID,@IsAuthority");
            builder.Append(" ) ");
            builder.Append(" ;select @@IDENTITY");
            builder.Append(" END");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AdminID", model.AdminID), new SqlParameter("@OrganID", model.OrganID), new SqlParameter("@IsAuthority", model.IsAuthority) };
            object single = DBHelper.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int AuthorityID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from AdminOrganAuthority ");
            builder.Append(" where AuthorityID=@AuthorityID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AuthorityID", SqlDbType.Int, 4) };
            cmdParms[0].Value = AuthorityID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteAll(int adminid)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from AdminOrganAuthority ");
            builder.Append(" where   AdminID= " + adminid.ToString());
            return (DBHelper.ExecuteSql(builder.ToString()) > 0);
        }

        public List<AdminOrganAuthority> GetList(int adminid)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append(" FROM AdminOrganAuthority ");
            builder.Append(" WHERE AdminID= ").Append(adminid);
            DataTable table = DBHelper.Query(builder.ToString()).Tables[0];
            AdminOrganAuthority item = null;
            List<AdminOrganAuthority> list = null;
            if (table.Rows.Count > 0)
            {
                list = new List<AdminOrganAuthority>();
                foreach (DataRow row in table.Rows)
                {
                    item = new AdminOrganAuthority();
                    if (row["AuthorityID"].ToString() != "")
                    {
                        item.AuthorityID = int.Parse(row["AuthorityID"].ToString());
                    }
                    if (row["IsAuthority"].ToString() != "")
                    {
                        item.IsAuthority = int.Parse(row["IsAuthority"].ToString());
                    }
                    if (row["AdminID"].ToString() != "")
                    {
                        item.AdminID = int.Parse(row["AdminID"].ToString());
                    }
                    if (row["OrganID"].ToString() != "")
                    {
                        item.OrganID = int.Parse(row["OrganID"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public AdminOrganAuthority GetModel(int AuthorityID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select   *  ");
            builder.Append("  from AdminOrganAuthority ");
            builder.Append(" where AuthorityID=@AuthorityID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AuthorityID", SqlDbType.Int, 4) };
            cmdParms[0].Value = AuthorityID;
            AdminOrganAuthority authority = new AdminOrganAuthority();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                DataRow row = set.Tables[0].Rows[0];
                if (row["AuthorityID"].ToString() != "")
                {
                    authority.AuthorityID = int.Parse(row["AuthorityID"].ToString());
                }
                if (row["IsAuthority"].ToString() != "")
                {
                    authority.IsAuthority = int.Parse(row["IsAuthority"].ToString());
                }
                if (row["AdminID"].ToString() != "")
                {
                    authority.AdminID = int.Parse(row["AdminID"].ToString());
                }
                if (row["OrganID"].ToString() != "")
                {
                    authority.OrganID = int.Parse(row["OrganID"].ToString());
                }
                return authority;
            }
            return null;
        }

        public bool Update(AdminOrganAuthority model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update AdminOrganAuthority set ");
            builder.Append(" AdminID = @AdminID , ");
            builder.Append(" IsAuthority = @IsAuthority , ");
            builder.Append(" OrganID = @OrganID  ");
            builder.Append(" where AuthorityID=@AuthorityID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AuthorityID", model.AuthorityID), new SqlParameter("@AdminID", model.AdminID), new SqlParameter("@IsAuthority", model.IsAuthority), new SqlParameter("@OrganID", model.OrganID) };
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

